package eerbaar.shared.persist.internal.lib;

import eerbaar.context.IParam;
import eerbaar.context.ParamFactory;
import eerbaar.shared.exceptions.GameRuntimeException;
import eerbaar.shared.persist.IPersister;
import eerbaar.shared.persist.ISaver;
import eerbaar.shared.tree.IAttributes;
import eerbaar.shared.tree.INode;

public class ParamPersister implements IPersister<IParam> {

    public static final String NAME = "param";

    private IParam target;

    public ParamPersister() {
        //
    }

    public ParamPersister(INode node) throws GameRuntimeException {
        load(node);
    }

    public ParamPersister(IParam wrapper) {
        this.target = wrapper;
    }

    @Override
    public Class<IParam> getRegistertedInterface() {
        return IParam.class;
    }

    @Override
    public ISaver<?>[] getSavableChildren() {

        return new ISaver[] {};
    }

    @Override
    public String getSavableName() {

        return NAME;
    }

    @Override
    public IParam getTarget() {
        return this.target;
    }

    @Override
    public boolean isAssignableFrom(ISaver<?> rsist) {
        return false;
    }

    @Override
    public IParam load(INode node) {
        assert (node != null);

        IAttributes attributes = node.getAttributes();
        String name = attributes.get(String.class, "name");
        String value = attributes.get(String.class, "value");
        IParam param = ParamFactory.createParam();
        param.setName(name);
        param.setValue(value);

        return param;
    }

    @Override
    public void saveAttributes(INode node) {

        IAttributes a = node.getAttributes();
        a.set(String.class, "name", this.target.getName());
        a.set(String.class, "value", this.target.getValue());
    }

    @Override
    public void setTarget(IParam obj) {
        this.target = obj;
    }

}
